const pool = require('../config/db');

class BugComment {
    // 添加 Bug 评论
    static async addComment(bugId, userId, content, commentType = 'comment') {
        try {
            const [result] = await pool.execute(
                'INSERT INTO bug_comments (bug_id, user_id, content, comment_type) VALUES (?, ?, ?, ?)',
                [bugId, userId, content, commentType]
            );
            return result.insertId;
        } catch (error) {
            console.error('添加Bug评论错误:', error);
            throw error;
        }
    }

    // 获取特定 Bug 的所有评论
    static async getCommentsByBugId(bugId) {
        try {
            const [comments] = await pool.execute(
                `SELECT 
                    bc.id, 
                    bc.bug_id, 
                    bc.user_id, 
                    u.username, 
                    u.real_name,
                    bc.content, 
                    bc.comment_type, 
                    bc.created_at
                FROM bug_comments bc
                JOIN users u ON bc.user_id = u.id
                WHERE bc.bug_id = ?
                ORDER BY bc.created_at DESC`,
                [bugId]
            );
            return comments;
        } catch (error) {
            console.error('获取Bug评论错误:', error);
            throw error;
        }
    }

    // 获取特定 Bug 的分页评论
    static async getCommentsByBugIdWithPage(bugId, page = 1, pageSize = 10) {
        try {
            const offset = (page - 1) * pageSize;

            // 获取总数
            const [countResult] = await pool.execute(
                'SELECT COUNT(*) as total FROM bug_comments WHERE bug_id = ?',
                [bugId]
            );
            const total = countResult[0].total;

            // 获取分页评论
            const [comments] = await pool.execute(
                `SELECT 
                    bc.id, 
                    bc.bug_id, 
                    bc.user_id, 
                    u.username, 
                    u.real_name,
                    bc.content, 
                    bc.comment_type, 
                    bc.created_at
                FROM bug_comments bc
                JOIN users u ON bc.user_id = u.id
                WHERE bc.bug_id = ?
                ORDER BY bc.created_at DESC
                LIMIT ? OFFSET ?`,
                [bugId, pageSize, offset]
            );

            return {
                total,
                comments,
                page,
                pageSize
            };
        } catch (error) {
            console.error('获取Bug评论错误:', error);
            throw error;
        }
    }

    // 获取单个评论详情
    static async getCommentDetail(commentId) {
        try {
            const [comments] = await pool.execute(
                `SELECT 
                    bc.id, 
                    bc.bug_id, 
                    bc.user_id, 
                    u.username, 
                    u.real_name,
                    bc.content, 
                    bc.comment_type, 
                    bc.created_at,
                    b.title as bug_title,
                    b.status as bug_status
                FROM bug_comments bc
                JOIN users u ON bc.user_id = u.id
                JOIN bugs b ON bc.bug_id = b.id
                WHERE bc.id = ?`,
                [commentId]
            );

            return comments.length > 0 ? comments[0] : null;
        } catch (error) {
            console.error('获取Bug评论详情错误:', error);
            throw error;
        }
    }

    // 更新评论
    static async updateComment(commentId, content) {
        try {
            const [result] = await pool.execute(
                'UPDATE bug_comments SET content = ? WHERE id = ?',
                [content, commentId]
            );
            return result.affectedRows > 0;
        } catch (error) {
            console.error('更新Bug评论错误:', error);
            throw error;
        }
    }

    // 删除评论
    static async deleteComment(commentId) {
        try {
            const [result] = await pool.execute(
                'DELETE FROM bug_comments WHERE id = ?',
                [commentId]
            );
            return result.affectedRows > 0;
        } catch (error) {
            console.error('删除Bug评论错误:', error);
            throw error;
        }
    }
}

module.exports = BugComment;
